/*
 * @Descripttion:
 * @Author: Silence
 * @Date: 2023-06-07 13:37:53
 * @LastEditors: Silence
 * @LastEditTime: 2023-06-09 09:20:33
 */
import { loginApi, getrolesApi, changerolesApi, getAsideApi, getuserApi, stateApi, adduserApi, deluserApi, changeuserApi } from '@/api/user.js'
import { getToken, setToken, delToken } from '@/utils/storage.js'
export default {
  state () {
    return {
      total: 0,
      orderList: [],
      token: getToken(),
      userlist: [],
      options: [],
      obj: {}
    }
  },
  mutations: {
    del (state) {
      delToken()
      state.token = ''
    },
    getData (state, data) {
      const arr = ['el-icon-s-custom', 'el-icon-box', 'el-icon-s-goods', 'el-icon-s-order', 'el-icon-s-marketing']
      state.orderList = data
      arr.forEach((item, index) => {
        state.orderList[index].class = item
      })
    },
    getuserData (state, data) {
      state.userlist = data.users
      state.total = data.total
    },
    getroles (state, arr) {
      state.options = arr
    },
    getDetail (state, data) {
      state.obj = data
    }
  },
  actions: {
    async syncLogin ({ commit }, data) {
      const res = await loginApi(data)
      setToken(res.data.token)
    },
    async getAsideData ({ commit }) {
      const res = await getAsideApi()
      // console.log(res, 121)
      commit('getData', res.data)
    },
    async getuserData ({ commit }, data) {
      const res = await getuserApi(data)
      // console.log(res)
      commit('getuserData', res.data)
    },
    changeState ({ commit }, obj) {
      return new Promise((resolve, reject) => {
        stateApi(obj.id, obj.bool).then(res => {
          resolve(res)
        })
      })
    },
    adduser ({ commit }, data) {
      return new Promise((resolve, reject) => {
        adduserApi(data).then(res => {
          resolve(res)
        })
      })
    },
    deluserinfo ({ commit }, id) {
      return new Promise((resolve, reject) => {
        deluserApi(id).then(res => {
          resolve(res)
        })
      })
    },
    changeuserData ({ commit }, obj) {
      return new Promise((resolve, reject) => {
        changeuserApi(obj.id, { email: obj.email, mobile: obj.mobile }).then(res => {
          resolve(res)
        })
      })
    },
    async getrolesData ({ commit }) {
      const res = await getrolesApi()
      commit('getroles', res.data)
    },
    changeRoleData ({ commit }, obj) {
      return new Promise((resolve, reject) => {
        changerolesApi(obj.id, obj.rid).then(res => {
          resolve(res)
        })
      })
    }
  },
  getters: {},
  namespaced: true
}
